<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportRentalController
 * @package Admin\Controller
 */
class ReportRentalController extends BaseController {


    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=-1; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    public function ajax_export(){

        header("Content-type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=租金统计报表.xls");  //文件下载

        $houseSt = array();
        $total = array("num"=>0, "size"=>0, "total"=>0);
        $year = I("request.year")?I("request.year"):date("Y");
        $this->action_GenerateReport($year, $houseSt, $total);

        echo '<table width="200" border="1">
                  <tr>
                    <td>统计小区</td>
                    <td>租金缴纳户数</td>
                    <td>出租房源面积</td>
                    <td>租金收缴金额</td>
                  </tr>';
        foreach($houseSt as $item){
            $tr = "
                <tr>
                <td>{{name}}</td>
                <td>{{num}}</td>
                <td>{{size}}</td>
                <td>{{total}}</td>
              </tr>";
            echo  strtr($tr, [
                "{{name}}" => $item['name'],
                "{{num}}" => $item['num'],
                "{{size}}" => $item['size'],
                "{{total}}" => $item['total'],
            ]);
        }
        $footer = "
                <tr>
                <td>总计：</td>
                <td>{{num}}</td>
                <td>{{size}}</td>
                <td>{{total}}</td>
              </tr>
            </table>";
        echo  strtr($footer, ["{{num}}" => $total['num'],"{{size}}" => $total['size'],"{{total}}" => $total['total']]);
    }

    private function action_GenerateReport($year, &$houseSt, &$total)
    {
        $where = array("p.state"=>"已确认");
        $where["p.year"] = $year;
        /*if(I("post.year")!="all"){
            $where["p.year"] = $year;
        }*/
        $community = M("community")->field("name")->select();
        foreach($community as $value){
            $houseSt[$value['name']] = array("name"=>$value['name'],"num"=>0,"size"=>0,"total"=>0);
        }
        $data = M("payments p")->where($where)
            ->field("p.community as name, count(p.house_no) as num, sum(h.size) as size, sum(p.amount) as total")
            ->join(C("DB_PREFIX")."house h on h.name = p.house_no", 'LEFT')
            ->group("p.community")
            ->select();
        foreach ($data as $item) {
            $houseSt[$item['name']] = $item;
            $total['num'] += $item['num'];
            $total['size'] += $item['size'];
            $total['total'] += $item['total'];
        }
        $houseSt = array_values($houseSt);
    }

        /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){
        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);
        $year = I("post.year")?I("post.year"):date("Y");
        $this->assign("year", $year);

        $houseSt = array();
        $total = array("num"=>0, "size"=>0, "total"=>0);
        $this->action_GenerateReport($year, $houseSt, $total);
        $this->assign("total", $total);
        $this->assign("rental", $houseSt);
        $this->display();
    }


}